有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从php页面中的java DataOutputStream接收输出

我有一个java小程序,我正在使用它将文件发送回我的服务器——在服务器端,我希望在php页面上接收这个文件

下面是执行发送的java代码,在php方面,我检查了全局数组,我有URL传递的数据,但没有文件数据。我真的在这一条上搜索和抓取了,所以感谢任何帮助

String strURL = sendToURL + "?ACTION=POST&LEN=" + imgBytes.length + "&Fname=picture.png";
    try{
        URL urlServlet = new URL(strURL);
        URLConnection sCon = urlServlet.openConnection();
        sCon.setDoInput(true);
        sCon.setDoOutput(true);
        if (sCon.getAllowUserInteraction()) {
            sCon.setAllowUserInteraction(true);
        }
        sCon.setUseCaches(false);
        sCon.setDefaultUseCaches(false);
        sCon.setRequestProperty("Content-Type", "text/html");
        sCon.setRequestProperty("Connection", "Keep-Alive");
        sCon.setConnectTimeout(transferTimeout);
        sCon.setReadTimeout(transferTimeout);
        DataOutputStream out = new DataOutputStream(sCon.getOutputStream());

        int index = 0;
        size = 1024;
        do {
            if (index + size > imgBytes.length) {
                size = imgBytes.length - index;
            }
            out.write(imgBytes, index, size);
            index += size;
        } while (index < imgBytes.length);

        out.write(imgBytes);
        out.flush();
        out.close();

解决了——正如经常发生的那样,一个人在经过几天的斗争后发布一个问题,仅仅几分钟后就会出现一个解决方案

我记得之前有一次使用cURL传输XML数据,在听到关于使用SOAP的评论后,我开始思考这个问题。几次搜索之后,我发现了一个更简单、更优雅的解决方案

http://www.lornajane.net/posts/2008/Accessing-Incoming-PUT-Data-from-PHP

基本上,您可以使用

file_get_contents("php://input")

所以现在它的效果非常好


共 (1) 个答案